package com.java.web.manageweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.java.base.pojo.EasyUIResult;
import com.java.manage.pojo.TbItemParam;
import com.java.manage.service.TbItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

/**
 * @ClassName ItemParamController
 * @Description 参数信息
 * @Author Trigger Wu
 * @Date 2018/9/1113:52
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/rest/item/param")
public class ItemParamController {
    @Autowired
    private TbItemParamService itemParamService;
    /*
     *  根据商品类目Id查询规格参数版本
     *
     * @author Tigger Wu
     * @date 2018/9/11 14:05
     * @param [itemCatId]
     * @return ResponseEntity<ItemParam>
     */
    @RequestMapping(value = "{itemCatId}", method = RequestMethod.GET)
    public ResponseEntity<TbItemParam> queryByItemCatId(@PathVariable("itemCatId") Long itemCatId) {
        try {
            EntityWrapper<TbItemParam> wrapper = new EntityWrapper<TbItemParam>();
            wrapper.where("item_cat_id={0}",itemCatId);
            TbItemParam itemParam = itemParamService.selectOne(wrapper);
            if (null == itemParam) {
                // 404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
    /*
     * 分页查询参数信息
     *
     * @author Tigger Wu
     * @date 2018/9/11 15:38
     * @param [page, rows]
     * @return org.springframework.http.ResponseEntity<com.java.base.pojo.EasyUIResult>
     */
    @RequestMapping(value ="/list", method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryAll(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "30") Integer rows) {
        try {
            Page<TbItemParam> itemPage = new Page<TbItemParam>(page,rows);
            EntityWrapper<TbItemParam> wrapper = new EntityWrapper<TbItemParam>();
            wrapper.orderBy("created",false);
            Page<TbItemParam> AllItemParam = itemParamService.selectPage(itemPage,wrapper);
            EasyUIResult ez = new EasyUIResult();
            ez.setRows(AllItemParam.getRecords());
            ez.setTotal(AllItemParam.getTotal());
            return ResponseEntity.ok(ez);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /*
     *  添加参数模板
     * 
     * @author Tigger Wu
     * @date 2018/9/11 14:13
     * @param [itemCatId, paramData]
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @RequestMapping(value = "{itemCatId}", method = RequestMethod.POST)
    public ResponseEntity<Void> saveItemParam(@PathVariable("itemCatId") Long itemCatId,
                                              @RequestParam("paramData") String paramData) {
        try {
            TbItemParam itemParam = new TbItemParam();
            itemParam.setId(null);
            itemParam.setItemCatId(itemCatId);
            itemParam.setParamData(paramData);
            Date d = new Date();
            itemParam.setCreated(d);
            itemParam.setUpdated(d);
            itemParamService.insert(itemParam);

            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
